package com.xwn.provider.handle;

import com.xwn.bean.enumBean.ResultEnum;
import com.xwn.bean.system.ResponseBodyEntity;
import com.xwn.bean.exception.BodyException;
import com.xwn.utils.utils.ResultUtil;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.JwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 异常统一拦截
 * @Author duandw
 * @Date 2019/11/5 15:24
 **/
@ControllerAdvice
@Slf4j
public class ExceptionHandle {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseBodyEntity handle(Exception e){
        if (e instanceof BodyException) {
            BodyException boyException = (BodyException) e;
            return ResultUtil.error(boyException.getCode(), boyException.getMessage());
        }else if(e instanceof ExpiredJwtException){
            return ResultUtil.error(ResultEnum.TOKEN_EXPIRED.getCode(), ResultEnum.TOKEN_EXPIRED.getMsg());

        }else if(e instanceof JwtException){
            return ResultUtil.error(ResultEnum.TOKEN_EXPIRED.getCode(), ResultEnum.TOKEN_EXPIRED.getMsg());

        }else {
            log.error("【系统异常】{}", e);
            return ResultUtil.error(ResultEnum.UNKONW_ERROR.getCode(), ResultEnum.UNKONW_ERROR.getMsg());
        }
    }
}
